const path = require('path')
  
  function resolve(dir) {
    return path.join(__dirname, dir)
  }

module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  configureWebpack: {
    entry: {
      app: './src/main.js'
    },
    name: '我爱背单词',
    resolve: {
      alias: {
        '@': resolve('src'),
        'src': resolve('src'),
        'components': resolve('src/components'),
        'pages': resolve('src/pages'),
        'store': resolve('src/store'),
        'services': resolve('src/services'),
        'utils': resolve('src/utils')
      }
    },
    // 添加调试配置
    devtool: 'source-map'
  },
  devServer: {
    port: 8081,
    open: true,
    client: {
      overlay: {
        warnings: true,
        errors: true
      },
      logging: 'verbose'
    },
    proxy: {
      '/api': {
        target: 'http://localhost:8082',
        changeOrigin: true,
        ws: true
      }
    }
  },
  chainWebpack(config) {
    // 设置svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // 设置gzip压缩
    config.plugin('compression')
      .use(require('compression-webpack-plugin'))
      .tap(() => [{
        test: /\.(js|css|html)$/,
        threshold: 10240
      }])

    // 配置externals，减少打包体积
    config.externals = {
      'vue': 'Vue',
      'axios': 'axios'
    }
  },
  pluginOptions: {
    'uni-app': {
      webpack: {
        // UniApp特有的配置
      }
    }
  }
}